import {observable, action, extendObservable, isObservable, toJS} from 'mobx';
// import WeaTableMobx from 'comsMobx';
// import ComMobx from '../../coms/wea-table';
import {WeaTableNew} from 'comsMobx'
const {TableStore} = WeaTableNew;

 
import {WeaForm, WeaSwitch} from 'comsMobx';
// import {WeaForm} from 'ecCom'

import * as API_WORKPLANSHAREPERSONAL from '../apis/workPlanSharePersonal';
import * as PLANMODULAPI  from '../apis/planModul';
import * as WORKPLANEXCHANGEAAPI from '../apis/workPlanExchange';
import * as SEARCHAPI from '../apis/search';

export default class CommonStore {

    constructor() {
        //用来获取weatable数据的接口，指向一个最终调用后会返回一个promise的方法
        //最后会被子类替换掉的
        this.interfaceOfGetData = null;

        //存储当前页面是什么页面，比如日程共享就存储'日程共享'
        this.pageName = '';

        //用于获取高级搜索的表单控件的数据的异步后台接口，根据页面和tab的切换会变
        this.interfaceOfGetCondition = null;

        this.interfaceOfDeleteTableData = null;



        // this.interfaceOfGetExchangeData = null;


    }

    @observable loading = false;

    @action
    setLoading(newBool=false) {
        this.loading = newBool
    }

    //根据不同的页面动态的设置不同页面所需要的后台接口
    setInterfaces = (nextViewcondition = 'none') => {
        if (this.pageName === 'workPlanSharePersonal') {
            if (nextViewcondition == '0') {
                this.interfaceOfGetCondition = API_WORKPLANSHAREPERSONAL.getSharePersonalCondition.bind(this);
                this.interfaceOfGetData = API_WORKPLANSHAREPERSONAL.getSharePersonalList.bind(this);
                this.interfaceOfDeleteTableData = API_WORKPLANSHAREPERSONAL.deleteDefaultShare.bind(this);
            } else {
                this.interfaceOfGetCondition = API_WORKPLANSHAREPERSONAL.getShareCreateCondition.bind(this);
                this.interfaceOfGetData = API_WORKPLANSHAREPERSONAL.getShareCreatelList.bind(this);
                this.interfaceOfDeleteTableData = API_WORKPLANSHAREPERSONAL.deleteCreateDefaultShare.bind(this);
            }
        } else if (this.pageName === 'planModul') {
            this.interfaceOfGetCondition = PLANMODULAPI.getCondition.bind(this);
            this.interfaceOfGetData = PLANMODULAPI.getlist.bind(this);
            this.interfaceOfDeleteTableData = PLANMODULAPI.deleteData.bind(this);
        } else if (this.pageName === 'workPlanExchange') {
            //待做
            this.interfaceOfGetCondition = WORKPLANEXCHANGEAAPI.getCondition.bind(this);
            this.interfaceOfGetData = WORKPLANEXCHANGEAAPI.getAllExchangeList.bind(this);
        } else if(this.pageName === 'WorkPlanSearch'){
            this.interfaceOfGetCondition = SEARCHAPI.getCondition.bind(this);
            this.interfaceOfGetData = SEARCHAPI.getlist.bind(this);
            this.interfaceOfbatchFinish = SEARCHAPI.batchFinish.bind(this, this.comsWeaTableStore.selectedRowKeys);
        }
    }

    //做的事情：1.根据tab的切换切换高级搜索的表单域 2.根据tab的切换显示不同的表格数据
    // params得是一个对象，结构为：
    //{
    // interfaceParams:表示用来传给后端异步接口用来获取后台数据的一个参数对象
    // CompParams:表示与当前组件相关的一些特殊参数的对象
    //     其中的viewcondition表示store.viewcondition即将变成这个viewcondition
    // }
    initData = (params={
        CompParams: {
            //代表切换tab后的vc，若组件调用此方法时没传入vc，则默认为当前的vc值而不变
            viewcondition: this.viewcondition,
            isFirstRendered: true
        }}) => {

        //在加载数据前先切换tab，防止切换tab的卡顿
        this.setViewcondition(params.CompParams.viewcondition);

        //在初始化数据开始前应该有进度条表示数据正在加载
        this.setLoading(true);


        // this.setPageName();
        this.setInterfaces(params.CompParams.viewcondition);
        this.interfaceOfGetCondition()
            //这里面是用来初始化高级搜素里的表单的
            .then(action('changeConditionInfo', (data) => {
                const {conditioninfo } = data;
                // WeaTools.ls.set(`list${this.baseParams.viewScope}LeftTree`, treedata);
                // this.title = pagetitle;
                // this.topTab = groupinfo;
                // this.leftTree = treedata;
                // this.leftTreeCountType = countcfg;
                // this.conditioninfo = conditioninfo;
                // // 根据高级搜索条件初始化form
                // this.form.initFormFields(conditioninfo);

                this.conditioninfo = conditioninfo;
                this.form = new WeaForm();
                // 根据高级搜索条件初始化form
                this.form.initFormFields(conditioninfo);

                if (this.pageName === "workPlanExchange") {
                    this.form.updateFields({
                        timeSag__startdate__enddate: {
                            value: ["" + params.CompParams.viewcondition]
                        }
                    });
                }
                //默认值设置待做

            }))
            .then(action('changeConditionInfoSuccess', () => {
                this.doSearch(params);
            }));
    };

    //表格
    @observable comsWeaTableStore = new TableStore();

    // 高级搜索的form
    @observable form = new WeaForm();

    //切换tab页时用来标识当前位于哪个tab的变量，none表示当前tab页没有tabs
    @observable viewcondition = 'none';

    @action setViewcondition(newVc) {
        this.viewcondition = newVc;
    }

    //存储高级搜索中用于显示所有表单控件的相关数据
    @observable conditioninfo = [];

    //用于控制传给weatable的sessionkey
    @observable dataKey = '0';

    /**
     * 工作交流用于分页特有的逻辑
     */

    //存储工作交流页面中每个tab上要显示的列表的相关数据
    @observable elements = [];

    //当前处于第几页
    @observable currentpage = 1;

    //每页显示条数
    @observable pagesize = 10;



    //数据总条数
    @observable count = 0;

    @action setPagesize = (nextPagesize) => {
        this.pagesize = nextPagesize
    }

    @action setCurrent = (nextCurrent, callback) => {
        this.currentpage = nextCurrent;
        typeof callback === 'function' && callback();
    }
    /**
     * 结束工作交流页面特有的逻辑
     * @type {Array}
     */



    /**
     * 查看日程详细或编辑日程相关交流的相关逻辑
     */

    //是否打开生成日程弹窗
    @observable ifShowCreateWP = false;

    //定义查看详细信息时的tab是哪个，
    //7是相关交流   1是基本信息
    @observable activeKey = '7';

    //查看日程详细时会用到的id
    @observable workPlanId = 0;

    @action
    setWorkPlanId(id) {
        this.workPlanId = id;
    }

    @action
    setLookType(newType) {
        this.lookType = newType;
    }

    @action setActiveKey(newKey) {
        this.activeKey = newKey;
    }

    //打开生成日程弹窗
    @action
    openCreateWP() {
        this.ifShowCreateWP = true;
    }

    //关闭生成日程弹窗
    @action
    closeCreateWP() {
        this.ifShowCreateWP = false;
    }
    /**
     * 结束查看或编辑日程详细相关逻辑
     */

    //用于控制高级搜索的显隐的变量
    @observable ifShowSearchAd = false;


    //控制新建或添加的弹窗的显隐
    @observable ifShowAddDialog = false;

    //这里拿到的bool是切换显隐前的显隐的bool值
    @action
    setShowSearchAd(bool) {
        this.ifShowSearchAd = bool;
    }


    //在普通搜索完成输入点击搜素时更新表单的默认值
    @action setFormFields(value){
        this.form.updateFields(value, true);    //true代表完全覆盖方式更新条件值
    }


    //打开新建或添加的弹窗
    @action openAddDialog() {
        this.ifShowAddDialog = true;
    }

    //关闭新建或添加的弹窗
    @action closeAddDialog() {
        this.ifShowAddDialog = false;
    }

    //根据动态的接口以及可选的interfaceParams获取并筛选要显示的表格数据，在获取表格数据成功并更新datakey后再调用回调
    //params得是一个对象，结构为：
    //{
    // interfaceParams:表示用来传给interfaceOfGetData的一个参数对象
    // CompParams:表示与当前组件相关的一些特殊参数的对象
    //      其中的viewcondition表示store.viewcondition即将变成这个viewcondition
    // }
    @action doSearch = (params={
        CompParams: {
            ////代表切换tab后的vc，若组件调用此方法时没传入vc，则默认为当前的vc值而不变
            viewcondition: this.viewcondition
        }
    }, cb) => {

        //在搜索开始前应该有进度条表示数据正在加载
        this.setLoading(true);


        //在加载数据前先切换tab，防止切换tab的卡顿
        this.setViewcondition(params.CompParams.viewcondition)

        let searchParamsAd = this.form.getFormParams();
        const searchParamsData = this.form.getFormDatas();
        let CompParams = params.CompParams;

        if (this.pageName === 'workPlanSharePersonal' || this.pageName === 'planModul'|| this.pageName === 'WorkPlanSearch') {

            this.interfaceOfGetData(searchParamsAd).then(action("fetchDataSuccess", data => {
                this.comsWeaTableStore.getDatas(data.sessionkey, CompParams.current || 1);
                this.dataKey = data.sessionkey;

                this.setLoading(false); //加载完数据清除进度条


                // this.viewcondition = CompParams.viewcondition;

                cb && cb(CompParams.viewcondition);

            }))
        } else if(this.pageName === 'workPlanExchange') {
            searchParamsAd = {
                timeSag: '0',
                ...searchParamsAd,
                currentpage: this.currentpage, //第几页
                pagesize: this.pagesize, //每页显示条数
            };
            this.interfaceOfGetData(searchParamsAd).then(action("fetchDataSuccess", data => {
                this.elements = data.datas;
                this.currentpage = data.currentpage;
                this.pagesize = data.pagesize;
                this.count = data.count;

                this.setLoading(false); //加载完数据清除进度条

                cb && cb(CompParams.viewcondition);

            }))
        }


    }


    // 获取form当前的值
    @action
    getFormParams() {
        return this.form.getFormParams();
    }

    //清空高级搜索表单里面所有字段的值
    @action resetAdSearchForm() {
        this.form.reset();
    }
}